<template>
  <div class="app-container">



    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="员工姓名" prop="userId">



        <el-input
          v-model="queryParams.userId"
          placeholder="请输入员工id"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      <!-- </el-form-item> -->
      <el-form-item label="学历" prop="education">
        <el-input
          v-model="queryParams.education"
          placeholder="请输入学历"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    
  
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['userdoc:doc:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['userdoc:doc:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['userdoc:doc:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['userdoc:doc:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="docList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
  
   
      <el-table-column label="员工姓名" align="center" prop="userName" />

      <el-table-column label="部门号" align="center" prop="dept" />
      <el-table-column label="职称" align="center" prop="jobtitle" />

      
      <el-table-column label="学历" align="center" prop="education" width="180">
        <template slot-scope="scope">
          <span>   {{ scope.row.education }} </span>
        </template>
      </el-table-column>

      

      <el-table-column label="生日" align="center" prop="birth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入职日期" align="center" prop="entryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="员工类型" align="center" prop="userType" width="180">
        <!-- <template slot-scope="scope">
          <span>   {{ scope.row.userType===1?"普通员工":"部门经理" }} </span>
        </template> -->
      </el-table-column>


      <el-table-column label="家庭住址" align="center" prop="address" width="180">
     
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['userdoc:doc:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['userdoc:doc:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工" prop="userId">
         
          <el-select v-model="form.userId" 
          ref="choose"
          placeholder="请输入员工" 

          @visible-change="openPerson"
       >
      
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
        </el-form-item>
     
        <el-form-item label="部门" prop="dept" >
          <el-input v-model="form.dept" placeholder="请输入员工部门" ref="dept"/>
        </el-form-item>

        <el-form-item label="职称" prop="职称">
          <el-input v-model="form.jobtitle" placeholder="请输入员工职称" />
        </el-form-item>



        <el-form-item label="学历" prop="education">
          <el-select v-model="form.education" placeholder="请输入学历" clearable>
            <el-option
            v-for="dict in dict.type.sys_user_degree"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
          </el-select>
         
          
        </el-form-item>
        <el-form-item label="生日" prop="birth">
          <el-date-picker clearable
            v-model="form.birth"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入职日期" prop="entryDate">
          <el-date-picker clearable
            v-model="form.entryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职日期">
          </el-date-picker>
        </el-form-item>
        
        <el-form-item label="员工类型" prop="userType">
        <el-select v-model="form.userType" placeholder="员工类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="家庭地址" address="">
          <el-input v-model="form.address" placeholder="请输入家庭地址" type="textarea"/>
        </el-form-item>

      

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<!-- 选人控件 -->
    <PeopleSelect ref="peopleSelect"   :isCheck="true" :open="peopleOpen" @cancel="peopleOpen=false"   @submit="submitPeople"></PeopleSelect>

  </div>
</template>

<script>
import { listDoc, getDoc, delDoc, addDoc, updateDoc } from "@/api/hrmanagement/doc";
import PeopleSelect from "@/components/PeopleSelect/index.vue";

export default {
  name: "Doc",
  dicts: ['sys_user_type','sys_user_degree','sys_normal_disable'],
  components: {
    PeopleSelect
  },
  data() {
    return {


      //是否打开选人组件，默认不打开
      peopleOpen:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工档案表格数据
      docList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
        //  员工下拉框
      options:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        education: null,
        birth: null,
        entryDate: null,
        userType: null,
        userNo: null,
        address:null,
        dept: null,
        jobtitle:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    

    /** 查询员工档案列表 */
    getList() {
      this.loading = true;
      listDoc(this.queryParams).then(response => {
        this.docList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        education: null,
        birth: null,
        entryDate: null,
        userType: null,
        createTime: null,
        updateTime: null,
        userNo: null,
        address:null,
        dept: null,
        jobtitle:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;

      this.title = "添加员工档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDoc(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDoc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDoc(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工档案编号为"' + ids + '"的数据项？').then(function() {
        return delDoc(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('userdoc/doc/export', {
        ...this.queryParams
      }, `doc_${new Date().getTime()}.xlsx`)
    },
        //打开选人弹窗
        openPerson(){
        this.peopleOpen=true;
        this.$refs.choose.blur();
       },
      //选择人的确定按钮事件 submitPeople(nikeNamelist)方法传参一个默认接收用户昵称数组   submitPeople(peopleList,nikeNamelist)方法传参两个则是接收用户昵称数组和用户账号数组
      submitPeople(peopleList,dept,peopleList1,peopleList2){
      // console.log(peopleList);
      // console.log(...dept);
      // console.log(peopleList1);
      // console.log(peopleList2);
      this.options = peopleList2
 
      this.form.dept =dept.toString();
// // 多选赋值
//       if (peopleList2.length>1 && peopleList2.length !== 0) {
        
//         this.form.userId= peopleList2.map(function(person) { 
//           return person.value;
//       });
//       }else{
//         this.form.userId=null  
//       }
// // 单选赋值
//       if ( peopleList2.length !== 0 && peopleList2.length === 1 ) {
//         this.form.userId = [peopleList2[0].value]
//       }
// 单选赋值
      if ( peopleList2.length !== 0 && peopleList2.length === 1 ) {
        this.form.userId = peopleList2[0].value
      }
      else{
        this.form.userId=null  
      }
console.log(this.form.userId);
      this.peopleOpen=false;
    }


  }
};
</script>
